#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{

      pid_t id = fork();
      if(id == 0)
      {
        int cnt = 3;
        while(cnt)
      {
            sleep(3);
            printf("我是一个子进程, pid : %d, ppid : %d\n", getpid(), getppid());
            sleep(1);
            cnt--;
            //int *p = 0;
            //*p= 100;
           // int a = 10;
           // a /= 0;
        }
        exit(10);
    }

    // 父进程
    while(1)
    {
        int status = 0;
        pid_t rid = waitpid(id, &status, 0);
        if(rid > 0)
        {
            printf("wait success, rid: %d, exit code: %d, exit signal: %d\n", rid, (status>>8)&0xFF, status&0x7F) ; // rid
            break;
        }
        else if(rid == 0)
        {
            // 函数指针进行回调处理
            int i = 0;
        
            printf("本轮调用结束，子进程没有退出\n");
            sleep(1);
        }
        else
        {
            printf("等待失败\n");
            break;
        }

    } 
    // int i = 4;
   //  
   // exit(1);
   // 
   // return 0;
}
